package com.example.springsecurity.util;


import io.jsonwebtoken.Claims;
import io.jsonwebtoken.Jwts;
import io.jsonwebtoken.security.Keys;

import java.nio.charset.StandardCharsets;
import java.security.Key;
import java.util.UUID;

public class JwtUtils {

    private final static String SECRET_KEY = UUID.randomUUID().toString();
    
    
    public static String createJwt(String userName){

       return Jwts.builder()
               .claim("userName",userName)
                .signWith(getKey()).compact();  
    }
    
    public static Claims parseJwt(String token){
        return Jwts.parser().setSigningKey(getKey()).parseClaimsJws(token).getBody();
    }
    
    private static Key getKey(){
        return Keys.hmacShaKeyFor(SECRET_KEY.getBytes(StandardCharsets.UTF_8));
    }

    public static void main(String[] args) {
        String jwt = createJwt("admin");
        System.out.println(jwt);
        Claims claims = parseJwt(jwt);
        System.out.println(claims);
    }
}
